
/* Performing functions of list in python by using linked lists in C.
3-3-2011 */

#include<stdio.h>

struct node
{
       int value;
       struct node *next;
}*ptr;

/* Creating a linked list */

create()
{
        int n,i;
        printf("HOW MANY NODES:");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            struct node *newnode;
            newnode=malloc(sizeof(struct node));
            printf("Enter the no.==>");
            scanf("%d",&newnode->value);                         
            newnode->next=ptr;
            ptr=newnode;
        }
}

/* Finding total no of nodes */

int no_of_nodes()
{
       int count=0;
       struct node *temp;
       temp=ptr;
       while(temp)
       {
                       
          temp=temp->next;
          count++;
       }
       return count;
      
}

/* Appending a node */

append()
{
        int i;
        
        struct node *temp;
        temp=ptr;
        struct node *newnode;        
        newnode=malloc(sizeof(struct node));
        printf("Enter the no.==>");
        scanf("%d",&newnode->value);
        for(i=1;i<no_of_nodes();i++)
        {
          temp=temp->next;
        }
        temp->next=newnode;
        temp=newnode;
        temp->next=NULL;
        
}
/* Insert a node at a given position */

insert()
{
        int i,l;        
        printf("Enter the location==>");
        scanf("%d",&l);
        struct node *curr;
        struct node *prev;
        curr=ptr;      
        struct node *newnode;        
        newnode=malloc(sizeof(struct node));
        printf("Enter the no.==>");
        scanf("%d",&newnode->value);
        for(i=1;i<l;i++)
        {
            prev=curr;    
            curr=curr->next;
                        
        }
        prev->next=newnode;
        prev=newnode;
        prev->next=curr;
              
}

/* Deleting a node */

remove_node()
{
        int num,i,count=0;
        struct node *curr;
        struct node *prev;
        curr=ptr;        
        printf("ENTER THE VALUE:");
        scanf("%d",&num);
        for(i==1;i<no_of_nodes();i++)
        {
             if((curr->value==num)&&(count==0))
                  {
                        ptr=ptr->next;
                  }
             if((curr->value==num)&&(count!=0))
                  {
                        prev->next=curr->next;
                        free(curr);                                                        
                  }
             else
                  {
                        prev=curr;
                        curr=curr->next;
                  }
             count++;
        }
}

/* Popping a node */

pop(int l)
{
        int i;        
        
        struct node *curr;
        struct node *prev;
        curr=ptr;                
        for(i=1;i<l;i++)
        {
                        prev=curr;    
                        curr=curr->next;
                        
        }
        prev->next=curr->next;
        free(curr);
}

/* Finding the no. of occurances of a number */

count()
{
       int count=0,num;
       printf("ENTER A NUMBER:");
       scanf("%d",&num);
       struct node *temp;
       temp=ptr;
       while(temp)
       {
                 if(temp->value==num)
                 {
                                     count++;
                 }
                 temp=temp->next;
       }
       printf("The no. %d occurs %d times in list\n",num,count);
}

/* Finding the index of a value */

index()
{
       int i,num,count=0;
       printf("ENTER A NUMBER:");
       scanf("%d",&num);
       struct node *temp;
       temp=ptr;
       while(temp)
       {
                  if(temp->value==num)
                  {
                                      printf("THE NUMBER %d IS AT INDEX %d",num,count+1);
                  }
                  count++;
                  temp=temp->next;
       }
       if(count==0)
       {
                   printf("NUMBER NOT FOUND");
       }
}

/*Reversing a list*/

reverse()
{
         struct node *temp;
         struct node *prev;
         struct node *curr;                           
         prev=NULL;       
         if(no_of_nodes()==0)
         {
                           printf("LIST IS NULL");
                           return;
         }     
         
         if(no_of_nodes()==1)
         {
                             return;
         }
         else
         {
             for(curr=ptr;curr!=NULL;)
             {
                                      temp=prev;
                                      prev=curr;
                                      curr=curr->next;
                                      prev->next=temp; 
             }
             ptr=prev;
             return;
                                 
         }  


}

/*Sorting a list*/

sort()
{
         int i,j,n=no_of_nodes(),num;
         struct node *curr;
         struct node *temp;
         curr=ptr;
         
         for(i=1;i<n;i++)
         {
                         temp=curr->next;
                         for(j=i+1;j<=n;j++)
                         {
                                           if(curr->value > temp->value)
                                           {
                                                          num=curr->value;
                                                          curr->value=temp->value;
                                                          temp->value=num;
                                           }
                                           temp=temp->next;
                         }
                         curr=curr->next;
         }
}
      
/*To display the linked list */

display()
{
         struct node *temp;
         temp=ptr;
         if(no_of_nodes()==0)
         {
                             printf("LIST IS EMPTY");
         }
         else
         {
             while(temp)
             {
                       printf(" >%d ",temp->value);
                       temp=temp->next;
             }
         }
}

int main()
{
      int ch;
      ptr=NULL;      
      while(1) 
      {
           printf("\n\n\n\n1.Create a list ==>\n");
           printf("2.Append a no.==>\n");
           printf("3.Extend a list==>\n");
           printf("4.Insert a no. at a particular location==>\n");         
           printf("5.Remove a node==>\n");
           printf("6.Pop a node==>\n");
           printf("7.Count the occurances of a no.==>\n");
           printf("8.Return the index of a given value==>\n");
           printf("9.Reverse a list ==>\n");
           printf("10.Sort a list==>\n");       
           printf("11.Display the list==>\n");
           printf("12.Exit==>\n\n\n");
           printf("\tEnter your choice(1-12)==>\n");
           scanf("%d",&ch);
           switch(ch)
           {
                case 1:
                     {
                          create();
                          display();
                          break;
                     }
                case 2:
                     {
                          append();
                          display();                                      
                          break;
                     }
                case 3:
                     {
                          int i,n;
                          printf("Enter the no. of nodes you want to extend==>");
                          scanf("%d",&n);
                          for(i=1;i<=n;i++)
                          append();
                          display();                                       
                          break;
                     }  
                case 4:
                     {
                          insert();
                          display();
                          break;
                     }
                case 5:
                     {
                          remove_node();
                          display();
                          break;                                                           
                     }
                case 6:
                     {
                          int l;
                          printf("Enter any location==>");
                          scanf("%d",&l);
                          if(l==0)
                                {
                                   pop(no_of_nodes());
                                               
                                }
                          else
                                {
                                   pop(l);                                          
                                                                                
                                }
                          display();
                          break; 
                     } 
                case 7:
                     {
                          count();
                          display();
                          break;                                       
                     }
                case 8:
                     {
                          index();                                       
                          break;                                       
                     }
                case 9:
                     {
                          reverse();                                       
                          break;
                                                                              
                     }
                case 10:
                     {
                          sort();                                       
                          break;
                                       
                     }
                case 11:
                     {
                          display();                                       
                          break;
                                                                                                                   
                     }
                case 12:
                     {
                          exit(0);                                      
                                                                                                                   
                     }
                             
            }
      }
}
